<?php
	if ($_GET['disc'] == "404") {
		$h_t = explode(":", $setupdata['base_href']);
		$httpx = strtolower($h_t[0]);
		
		if (isset($_SERVER['HTTP_X_REWRITE_URL']) && strlen($_SERVER['HTTP_X_REWRITE_URL'])>0) {
			$url = substr($_SERVER['HTTP_X_REWRITE_URL'], 1, strlen($_SERVER['HTTP_X_REWRITE_URL'])-1);
		} else {
			if (isset($_SERVER['DOCUMENT_ROOT'])) {
				$res = substr($_SERVER['DOCUMENT_ROOT'], 0, 1);
				if ($res == '/'){
					$url = strtolower($httpx."://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
					$url = @ereg_replace("^".strtolower($setupdata['base_href']."/"), "", $url);
				} else {
					$url = strtolower($_SERVER['REQUEST_URI']);
					$url = @ereg_replace("^".strtolower($setupdata['site_dir']), "", $url);
				}
			} else {
				$url = strtolower($httpx."://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
				$url = @ereg_replace("^".strtolower($setupdata['base_href']."/"), "", $url);
			}
		}
		
		if (strpos($url, "_tvs_system_core_/mce/tiny_gzip.php") !== FALSE) {
			header("HTTP/1.0 200 Ok");
			include $setupdata['core_dir']."/mce/tiny_gzip.php";
			$mysqlclass->close_db();
			include $setupdata['core_dir']."/includes/die.php";
		}
				
		if ($url == "admin") {
			$mysqlclass->close_db();
			session_write_close();
			header ("location: index.php?admin=1");
			include $setupdata['core_dir']."/includes/die.php";
		}

		if (strpos($url, "favicon.ico") !== FALSE){
			$mysqlclass->close_db();
			header("HTTP/1.0 404 Not Found");
			include $setupdata['core_dir']."/includes/die.php";
		}
		
		if (strpos($url, "robots.txt") !== FALSE) {
			$mysqlclass->close_db();
			header("HTTP/1.0 200 Ok");
			header("Content-type: text/plain");
			echo "User-agent: *\r\nDisallow: /admin/";
			include $setupdata['core_dir']."/includes/die.php";
		}
		
		if (strpos($url, "ttf.php") !== FALSE) {
			header("HTTP/1.0 200 Ok");
			include $setupdata['core_dir']."/includes/ttftext.php";
			$mysqlclass->close_db();
			include $setupdata['core_dir']."/includes/die.php";
		}
		
		if (strpos($url, "spacer.gif") !== FALSE) {
			header("HTTP/1.0 200 Ok");
			header("Content-type: image/gif");
			header("Content-Length: 43");
			print(base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="));
			$mysqlclass->close_db();
			include $setupdata['core_dir']."/includes/die.php";
		}

		if (strpos($url, "_tvs_system_core_/mce") !== FALSE){
			header("HTTP/1.0 200 Ok");
			header("Status: 200 OK");
			$t = substr($url, strlen("_tvs_system_core_/mce"), strlen($url)-strlen("_tvs_system_core_/mce"));
			$x = explode('?', $t, 2);
			$t = $x[0];
			$path = $setupdata['core_dir']."/mce".$t;
			$t1 = explode("/", $path);
			if (isset($x[1]) && (strlen($x[1]) > 0)) $name = $t1[count($t1)-1]."?".$x[1];
			else $name = $t1[count($t1)-1];
			if (is_file($path)){
				header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($path))." GMT");
				header("Content-Length: ".filesize($path));
				header("Content-disposition: inline; filename=".$name);
				$handle = fopen($path, "rb");
				while (ob_get_level()) ob_end_clean();
				if ($handle !== false){
					while (!feof($handle)) echo fread($handle, 1024);
					fclose($handle);
				}
				$mysqlclass->close_db();
				exit();
			}
		}

		if (strpos($url, "_tvs_system_core_/design") !== FALSE){
			header("HTTP/1.0 200 Ok");
			$t = substr($url, strlen("_tvs_system_core_/design"), strlen($url)-strlen("_tvs_system_core_/design"));
			$path = $setupdata['core_dir']."/design".$t;
			if (is_file($path)){
				header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($path))." GMT");
				header("Content-type: image/jpeg");
				header("Content-Length: ".filesize($path));
				$handle = fopen($path, "rb");
				while (ob_get_level()) ob_end_clean();
				if ($handle !== false){
					while (!feof($handle)) echo fread($handle, 1024);
					fclose($handle);
				}
				$mysqlclass->close_db();
				exit();
			}
		}

		if (strpos($url, "_tvs_system_core_/js") !== FALSE){
			header("HTTP/1.0 200 Ok");
			$t = substr($url, strlen("_tvs_system_core_/js"), strlen($url)-strlen("_tvs_system_core_/js"));
			$path = $setupdata['core_dir']."/js".$t;
			if (is_file($path)){
				$mysqlclass->close_db();
				header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($path))." GMT");
				header("Content-type: application/x-js");
				header("Content-Length: ".filesize($path));
				$handle = fopen($path, "r");
				while (ob_get_level()) ob_end_clean();
				if ($handle !== false){
					while (!feof($handle)) echo fread($handle, 1024);
					fclose($handle);
				}
				exit();
			}
		}

		if ((strpos($url, "sitemap.xml") !== FALSE) || (strpos($url, "sitemap.xml.gz") !== FALSE)){
			if (isset($setupdata['google_sitemap']) && (is_file($setupdata['google_sitemap']))){
				$handle = fopen($setupdata['google_sitemap'], "rb");
				if (strpos($url, "sitemap.xml.gz") !== FALSE){
					$tmpfname = $setupdata['smarty_compile_dir']."sitemap.xml.gz";
					$fp = @fopen("compress.zlib://{$tmpfname}", 'wb');
					while (!feof($handle)) fwrite($fp, fread($handle, 1024));
					fclose($handle);
					fclose($fp);
					$mysqlclass->close_db();
					header("HTTP/1.0 200 Ok");
					header("Pragma: public");
					header("Expires: 0");
					header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
					header("Cache-Control: private", false);
					header('Content-type: application/x-gzip-compressed');
					header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
					header("Content-Transfer-Encoding: binary");
					header('Content-length: '.filesize($tmpfname));
					$fp = fopen($tmpfname, 'rb');
					while (ob_get_level()) ob_end_clean();
					while (!feof($fp)) echo fread($fp, 1024);
					fclose($fp);
					exit();
				} else {
					$mysqlclass->close_db();
					header("HTTP/1.0 200 Ok");
					while (ob_get_level()) ob_end_clean();
					while (!feof($handle)) echo fread($handle, 1024);
					fclose($handle);
					exit();
				}
				$mysqlclass->close_db();
				exit();
			}
		}
		
		if ((strpos($url, "urllist.txt") !== FALSE) || (strpos($url, "urllist.gz") !== FALSE)){
			if (isset($setupdata['google_sitemap']) && (is_file($setupdata['google_sitemap']))){
				$handle = fopen($setupdata['google_sitemap'], "rb");
				$buffer = ''; 
				while (!feof($handle)) $buffer .= fread($handle, 1024); 
				fclose($handle); 
				preg_match_all("'<loc>.*?</loc>'si", $buffer, $content, PREG_SET_ORDER);
				$handle = fopen($setupdata['smarty_compile_dir']."urllist.txt", 'wb');
				foreach ($content as $val) {
					$val[0] = strip_tags($val[0]);
					fwrite($handle, $val[0]);
					fwrite($handle, "\n");
				}
				fclose($handle);
				$handle = fopen($setupdata['smarty_compile_dir']."urllist.txt", "rb");
				if (strpos($url, "urllist.gz") !== FALSE){
					$mysqlclass->close_db();
					$tmpfname = $setupdata['smarty_compile_dir']."urllist.gz";
					$fp = @fopen("compress.zlib://{$tmpfname}", 'wb');
					while (!feof($handle)) fwrite($fp, fread($handle, 1024));
					fclose($handle);
					fclose($fp);
					header("HTTP/1.0 200 Ok");
					header("Pragma: public");
					header("Expires: 0");
					header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
					header("Cache-Control: private", false);
					header('Content-type: application/x-gzip-compressed');
					header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
					header("Content-Transfer-Encoding: binary");
					header('Content-length: '.filesize($tmpfname));
					$fp = fopen($tmpfname, 'rb');
					while (ob_get_level()) ob_end_clean();
					while (!feof($fp)) echo fread($fp, 1024);
					fclose($fp);
					exit();
				} else {
					$mysqlclass->close_db();
					header("HTTP/1.0 200 Ok");
					while (ob_get_level()) ob_end_clean();
					while (!feof($handle)) echo fread($handle, 1024);
					fclose($handle);
					exit();
				}
				$mysqlclass->close_db();
				exit();
			}
		}

		$tmp = explode(".", $url);
		$end = strtolower($tmp[(count($tmp)-1)]);
		if (in_array($end, $setupdata['vars']['no_error_page_file_ext'])) {
			header("HTTP/1.0 404 Not Found");
			$mysqlclass->close_db();
			exit();
		}
		unset($end);
			
// www
//		$url = strtolower("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
//		$url = ereg_replace("^".strtolower($setupdata['base_href']."/"), "", $url);

// localhost
//		$url = strtolower($_SERVER['REQUEST_URI']);
//		$url = ereg_replace("^".strtolower($setupdata['site_dir']), "", $url);

		$tmp = explode("/", $url);
		
		if (count($tmp) == 1){
			$tmp2 = explode("?", $tmp[0]);
			if (count($tmp2) == 1) {
				$dir1 = $tmp2[0];
				$id = 0;
			} else {
				$dir1 = $tmp2[0];
				$dir2 = $tmp2[1];
			}
		} else if (count($tmp) > 1){
			$dir1 = $tmp[0];
			$dir2 = implode("/", array_slice(explode("/", $url),1));
		}

		/*
		$lg = $treeclass->gettreetypes('lang');
		$lng = array();
		foreach ($lg as $val) array_push($lng, strtolower($val['name']));
		$lng = $treeclass->get_site_active_langs($lng);	
		*/
		
		$lng = $treeclass->active_langs;
		
		if (in_array($dir1, $lng)) {
			$lang = $dir1;
			$_SESSION['lang'] = $lang;
			$is_set_lang = 1;
		} else {
			if ((!(isset($_SESSION['lang']))) or ($_SESSION['lang'] == "") or ($_SESSION['lang'] == "0")) {
				$lang = $setupdata['default_languages'];
				$_SESSION['lang'] = $lang;
				$is_set_lang = 1;
			} else $lang = $_SESSION['lang'];
		}
		
		if ($lang != $dir1) $dir2 = $url;
		else if (!(isset($dir2))) $dir2 = "";
		$tmp = explode("?", $dir2);
		
		$dlink = @ereg_replace("/$", "", $tmp[0]);

		$param = $_SERVER['REQUEST_URI'];
		$tmp2 = explode("?", $param);
		$param = $tmp2[count($tmp2)-1];
		
		//$param = $tmp[count($tmp)-1];

		$tmp2 = explode("&", $param);
		foreach ($tmp2 as $value) {
			$tmp = explode("=", $value);
			if (isset($tmp[1])) $_GET[$tmp[0]] = $tmp[1];
			else $_GET[$tmp[0]] = NULL;
		}

		$tree = get_tree_mas($setupdata, $lang);
		$tree_all = $tree['tree_all'];
		$tree = $tree['tree'];
		
		if ((isset($dlink)) and (strlen($dlink)>0)) {
			$id = $treeclass->get_id_by_dlink($dlink, $lang, $tree);
		}
	
		header("HTTP/1.0 200 Ok");
		$_GET['lang'] = $lang;
		$_SESSION['lang'] = $lang;
		
		if (isset($id) and ($id > 0)) {
			$info = $treeclass->get_field_value($id, 'static_url');
			if ((isset($info)) and (strlen($info)>0)) {
				$mysqlclass->close_db();
				session_write_close();
				header ("location: ".$info);
				exit();
			} else $_GET['id'] = $id;
		} else {
			if (!isset($is_set_lang) ||  (isset($is_set_lang) && (isset($dlink)) && (strlen($dlink)>0))){
				$site = $treeclass->_get_site_settings();
				while (ob_get_level()) ob_end_clean();
				$mysqlclass->close_db();
				if (isset($site['site_settings_404_error']) && strlen($site['site_settings_404_error'])>0){
					header("HTTP/1.0 301 Moved Permanently");
          session_write_close();
					header('Location: '.$site['site_settings_404_error']);
					include $setupdata['core_dir']."/includes/die.php";
				} else {
					header("HTTP/1.0 404 Not Found");
					include $setupdata['core_dir']."/includes/die.php";
				}
			} elseif (isset($is_set_lang)) {
				$_GET['id'] = 0;
			}
		}
	} else {
		print_r("MYSQL ERROR");
		$mysqlclass->close_db();
		include $setupdata['core_dir']."/includes/die.php";
	}
?>
